<template>
  <div>
    <div id="multiPie" ref="multiPie" style="height:380px;margin-bottom:16px;"></div>
  </div>
</template>

<script>
import echarts from 'echarts'
export default {
  name: 'DischargeBarChart',
  data () {
    return {
      mychart: {}
    }
  },
  mounted () {
    this.initChart()
    window.addEventListener('resize', () => {
      setTimeout(() => {
        this.mychart.resize()
      }, 500)
    })
  },
  methods: {
    initChart () {
      const data = [{
        'name': '2019/1',
        'value': 60
      }, {
        'name': '2019/2',
        'value': 87.8
      }, {
        'name': '2019/3',
        'value': 71
      }, {
        'name': '2019/4',
        'value': 88
      }, {
        'name': '2019/5',
        'value': 66
      }, {
        'name': '2019/6',
        'value': 80
      }]
      const xData = []
      const yData = []
      const min = 0
      data.map(function (a, b) {
        xData.push(a.name)
        if (a.value === 0) {
          yData.push(a.value + min)
        } else {
          yData.push(a.value)
        }
      })
      this.mychart = echarts.init(this.$refs.multiPie)
      const option = {
        backgroundColor: 'rgb(0,0,0,.9)',
        color: ['#03C148'],
        title: {
          text: '社会效益（减少排放）',
          top: '0',
          left: '10',
          textStyle: {
            color: '#fff',
            fontSize: '14'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            lineStyle: {
              opacity: 0
            }
          },
          formatter: function (prams) {
            if (prams[0].data === min) {
              return '减少排放量：0%'
            } else {
              return '减少排放量：' + prams[0].data + '%'
            }
          }
        },
        legend: {
          data: ['直接访问', '背景'],
          show: false
        },
        grid: {
          left: '0%',
          right: '0%',
          bottom: '5%',
          top: '7%',
          height: '85%',
          containLabel: true,
          z: 22
        },
        xAxis: [{
          type: 'category',
          gridIndex: 0,
          data: xData,
          axisTick: {
            alignWithLabel: true
          },
          axisLine: {
            lineStyle: {
              color: '#0c3b71'
            }
          },
          axisLabel: {
            show: true,
            color: 'rgb(170,170,170)',
            fontSize: 16
          }
        }],
        yAxis: [{
          type: 'value',
          gridIndex: 0,
          splitLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          min: min,
          max: 100,
          axisLine: {
            lineStyle: {
              color: '#0c3b71'
            }
          },
          axisLabel: {
            color: 'rgb(170,170,170)',
            formatter: '{value} %'
          }
        },
        {
          type: 'value',
          gridIndex: 0,
          min: min,
          max: 100,
          splitNumber: 12,
          splitLine: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            show: false
          },
          splitArea: {
            show: true,
            areaStyle: {
              color: ['rgba(250,250,250,0.0)', 'rgba(250,250,250,0.05)']
            }
          }
        }
        ],
        series: [{
          name: '合格率',
          type: 'bar',
          barWidth: '30%',
          xAxisIndex: 0,
          yAxisIndex: 0,
          itemStyle: {
            normal: {
              barBorderRadius: 30,
              color: new echarts.graphic.LinearGradient(
                0, 0, 0, 1, [{
                  offset: 0,
                  color: '#00feff'
                },
                {
                  offset: 0.5,
                  color: '#027eff'
                },
                {
                  offset: 1,
                  color: '#0286ff'
                }
                ]
              )
            }
          },
          data: yData,
          zlevel: 11
        },
        {
          name: '背景',
          type: 'bar',
          barWidth: '50%',
          xAxisIndex: 0,
          yAxisIndex: 1,
          barGap: '-135%',
          data: [100, 100, 100, 100, 100, 100, 100],
          itemStyle: {
            normal: {
              color: 'rgba(255,255,255,0.1)'
            }
          },
          zlevel: 9
        }
        ]
      }
      this.mychart.setOption(option)
    },
    getArrayValue (array, key) {
      key = key || 'value'
      var res = []
      if (array) {
        array.forEach(function (t) {
          res.push(t[key])
        })
      }
      return res
    },
    getData (data) {
      const arrValue = this.getArrayValue(data, 'value')
      const sumValue = this.evil(arrValue.join('+'))
      var res = {
        series: [],
        yAxis: []
      }
      for (let i = 0; i < data.length; i++) {
        res.series.push({
          name: '比例',
          type: 'pie',
          clockWise: false, // 顺时加载
          hoverAnimation: false, // 鼠标移入变大
          radius: [65 - i * 15 + '%', 57 - i * 15 + '%'],
          center: ['50%', '55%'],
          label: {
            show: false
          },
          itemStyle: {
            label: {
              show: false
            },
            labelLine: {
              show: false
            },
            borderWidth: 5
          },
          data: [
            {
              value: data[i].value,
              name: data[i].name
            },
            {
              value: sumValue - data[i].value,
              name: '',
              itemStyle: {
                color: 'rgba(0,0,0,0)',
                borderWidth: 0
              },
              tooltip: {
                show: false
              },
              hoverAnimation: false
            }
          ]
        })
        res.series.push({
          name: '',
          type: 'pie',
          silent: true,
          z: 1,
          clockWise: false, // 顺时加载
          hoverAnimation: false, // 鼠标移入变大
          radius: [65 - i * 15 + '%', 57 - i * 15 + '%'],
          center: ['50%', '55%'],
          label: {
            show: false
          },
          itemStyle: {
            label: {
              show: false
            },
            labelLine: {
              show: false
            },
            borderWidth: 5
          },
          data: [
            {
              value: 7.5,
              itemStyle: {
                color: '#E3F0FF',
                borderWidth: 0
              },
              tooltip: {
                show: false
              },
              hoverAnimation: false
            },
            {
              value: 2.5,
              name: '',
              itemStyle: {
                color: 'rgba(0,0,0,0)',
                borderWidth: 0
              },
              tooltip: {
                show: false
              },
              hoverAnimation: false
            }
          ]
        })
        res.yAxis.push(data[i].name + ' ' + ((data[i].value / sumValue) * 100).toFixed(2) + '%')
      }
      return res
    },
    evil (fn) {
      // 一个变量指向Function，防止有些前端编译工具报错
      const Fn = Function
      return new Fn('return ' + fn)()
    }
  }
}
</script>

<style lang="less" scoped></style>
